package bb.lanxing.devices.sync;

import android.util.Log;
import androidx.collection.LongSparseArray;
import bb.lanxing.lib.devices.core.sync.DeviceFile;
import bb.lanxing.lib.devices.core.sync.DeviceSyncListener;
import bb.lanxing.lib.devices.core.sync.SyncManager;
import bb.lanxing.lib.devices.core.sync.SyncStatusNotifier;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
@Deprecated
abstract class AbsSyncManager implements SyncManager {
    private static int ID = 1;
    private DeviceFile mCurrentFile;
    private List<DeviceFile> mDeviceFiles;
    private LinkedList<DeviceFile> mFileQueue;
    private boolean mIsSyncWithMultiFiles;
    private SyncStatusNotifier mNotifier;
    private LongSparseArray<Integer> mStates;
    private int mSyncQueueIndex;
    private Object obj;

    @Override
    public void abort() {
    }

    @Override
    public void abortAll() {
    }

    @Override
    public void delete(long j) {
    }

    @Override
    public void deleteAll() {
    }

    abstract void doReadFileList();

    abstract boolean doSync(DeviceFile deviceFile);

    protected boolean doUpload(DeviceFile deviceFile) {
        return false;
    }

    protected boolean exists(long j) {
        return false;
    }

    @Override
    public String getPath(long j) {
        return null;
    }

    @Override
    public boolean isSynchronised(long j) {
        return false;
    }

    @Override
    public boolean resync(DeviceFile deviceFile) {
        return false;
    }

    protected boolean shouldSyncWithServer() {
        return false;
    }

    protected void showNotificationAfterFinish(int i, int i2) {
    }

    @Override
    public boolean sync(String str) {
        return false;
    }

    @Override
    public void readFileList() {
        if (this.mDeviceFiles != null && isSynchronising()) {
            notifyReadFileList(this.mDeviceFiles);
        } else {
            doReadFileList();
        }
    }

    @Override
    public int getSyncState(long j) {
        LongSparseArray<Integer> longSparseArray = this.mStates;
        int intValue = longSparseArray != null ? longSparseArray.get(j, -1) : -1;
        if (intValue != -1 || !exists(j)) {
            return intValue;
        }
        return 3;
    }

    @Override
    public boolean sync(DeviceFile deviceFile) {
        if (isSynchronising()) {
            return false;
        }
        this.mIsSyncWithMultiFiles = false;
        return syncImpl(deviceFile);
    }

    @Override
    public boolean sync(long j) {
        List<DeviceFile> list;
        if (!isSynchronising() && (list = this.mDeviceFiles) != null) {
            for (DeviceFile deviceFile : list) {
                if (deviceFile.getId() == j) {
                    return sync(deviceFile);
                }
            }
        }
        return false;
    }

    @Override
    public boolean sync() {
        DeviceFile deviceFile;
        if (isSynchronising()) {
            return false;
        }
        this.mIsSyncWithMultiFiles = true;
        LinkedList<DeviceFile> linkedList = this.mFileQueue;
        if (linkedList == null) {
            this.mFileQueue = new LinkedList<>();
        } else {
            linkedList.clear();
        }
        List<DeviceFile> list = this.mDeviceFiles;
        if (list != null) {
            for (DeviceFile deviceFile2 : list) {
                if (!exists(deviceFile2.getId())) {
                    this.mFileQueue.add(deviceFile2);
                    notifySyncStatus(deviceFile2.getId(), 5);
                }
            }
        }
        this.mSyncQueueIndex = 0;
        if (this.mFileQueue.isEmpty()) {
            deviceFile = null;
        } else {
            LinkedList<DeviceFile> linkedList2 = this.mFileQueue;
            int i = this.mSyncQueueIndex;
            this.mSyncQueueIndex = i + 1;
            deviceFile = linkedList2.get(i);
        }
        return deviceFile != null && syncImpl(deviceFile);
    }

    protected boolean syncImpl(DeviceFile deviceFile) {
        this.mCurrentFile = deviceFile;
        if (doSync(deviceFile)) {
            notifySyncStatus(deviceFile.getId(), 1);
            return true;
        }
        this.mCurrentFile = null;
        notifySyncStatus(deviceFile.getId(), 4);
        return false;
    }

    public DeviceFile getFileById(long j) {
        List<DeviceFile> list = this.mDeviceFiles;
        if (list != null) {
            for (DeviceFile deviceFile : list) {
                if (j == deviceFile.getId()) {
                    return deviceFile;
                }
            }
            return null;
        }
        return null;
    }

    public void removeById(long j) {
        if (this.mDeviceFiles != null) {
            this.mDeviceFiles.remove(getFileById(j));
        }
    }

    private void checkToSyncWithNextRecord(int i) {
        int i2;
        if (i == 3 || i == 4) {
            LinkedList<DeviceFile> linkedList = this.mFileQueue;
            if (linkedList != null && this.mSyncQueueIndex < linkedList.size()) {
                LinkedList<DeviceFile> linkedList2 = this.mFileQueue;
                int i3 = this.mSyncQueueIndex;
                this.mSyncQueueIndex = i3 + 1;
                syncImpl(linkedList2.get(i3));
                return;
            }
            if (isSynchronisingWithMultiFiles()) {
                LinkedList<DeviceFile> linkedList3 = this.mFileQueue;
                int i4 = 0;
                if (linkedList3 != null) {
                    Iterator<DeviceFile> it = linkedList3.iterator();
                    i2 = 0;
                    while (it.hasNext()) {
                        int syncState = getSyncState(it.next().getId());
                        if (syncState == 3) {
                            i4++;
                        } else if (syncState == 4) {
                            i2++;
                        }
                    }
                } else {
                    i2 = 0;
                }
                showNotificationAfterFinish(i4, i2);
            }
            reset();
        }
    }

    public DeviceFile getCurrentItem() {
        return this.mCurrentFile;
    }

    public void setCurrentFile(DeviceFile deviceFile) {
        this.mCurrentFile = deviceFile;
    }

    public DeviceFile getItem(long j) {
        List<DeviceFile> list = this.mDeviceFiles;
        if (list != null) {
            for (DeviceFile deviceFile : list) {
                if (deviceFile.getId() == j) {
                    return deviceFile;
                }
            }
            return null;
        }
        return null;
    }

    @Override
    public boolean isSynchronising() {
        LinkedList<DeviceFile> linkedList;
        return this.mCurrentFile != null || ((linkedList = this.mFileQueue) != null && this.mSyncQueueIndex < linkedList.size());
    }

    @Override
    public boolean isSynchronisingWithMultiFiles() {
        return this.mIsSyncWithMultiFiles;
    }

    public void notifySyncStatus(long j, int i) {
        DeviceFile fileById = getFileById(j);
        if (fileById != null) {
            notifySyncStatus(fileById, i, (String) null, true);
        }
    }

    public void notifySyncStatus(long j, int i, String str, boolean z) {
        DeviceFile fileById = getFileById(j);
        if (fileById != null) {
            notifySyncStatus(fileById, i, str, z);
        }
    }

    void notifySyncStatus(DeviceFile deviceFile, int i, String str, boolean z) {
        if (this.mStates == null) {
            this.mStates = new LongSparseArray<>();
        }
        if (z) {
            this.mStates.put(deviceFile.getId(), i);
        }
        checkToSyncWithNextRecord(i);
        if (deviceFile != null) {
            this.mNotifier.notifySyncStatus(deviceFile, i, str);
        }
    }

    public void notifyProgressUpdate(long j, float f) {
        DeviceFile fileById = getFileById(j);
        if (fileById != null) {
            Log.d("wangsm_bt", " AbsSyncManager.java : notifyProgressUpdate");
            this.mNotifier.notifyProgressUpdate(fileById, f);
        }
    }

    public void notifyReadFileList(List<DeviceFile> list) {
        if (list != null) {
            this.mDeviceFiles = list;
        }
        this.mNotifier.notifyReadFileList(list);
    }

    public void reset() {
        this.mCurrentFile = null;
    }

    @Override
    public void unregisterSyncListener(DeviceSyncListener deviceSyncListener) {
        SyncStatusNotifier syncStatusNotifier = this.mNotifier;
        if (syncStatusNotifier != null) {
            syncStatusNotifier.unregisterSyncListener(deviceSyncListener);
        }
    }

    @Override
    public void registerSyncListener(DeviceSyncListener deviceSyncListener) {
        if (this.mNotifier == null) {
            this.mNotifier = new SyncStatusNotifier();
        }
        this.mNotifier.registerSyncListener(deviceSyncListener);
    }

    @Override
    public Object getTag() {
        return this.obj;
    }

    @Override
    public void setTag(Object obj) {
        this.obj = obj;
    }

    @Override
    public void release() {
        SyncStatusNotifier syncStatusNotifier = this.mNotifier;
        if (syncStatusNotifier != null) {
            syncStatusNotifier.release();
        }
        this.obj = null;
    }

    protected void d(String str) {
        Log.d("sync-manager", str);
    }

    public void e(Throwable th) {
        Log.e("sync-manager", Log.getStackTraceString(th));
    }

    public static long generateFileId() {
        int i = ID;
        ID = i + 1;
        return i % Long.MAX_VALUE;
    }
}
